package screen;

import main.GameCanvas;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import model.Command;
import model.IAction;
import model.Item;
import model.MyObj;
import model.Position;
import model.Quest;
import utilities.CRes;
import utilities.GameResource;
import font.BitmapFont;

public class ListQuestScr extends Screen{
	static ListQuestScr instance;
	public static ListQuestScr gI(){
		if(instance==null)
			instance=new ListQuestScr();
		return instance;
	}
	
	Command cmdClose;
	public Vector m_items;
	public Screen backScreen;
	private int m_heightListView;
	private int m_bonusMoney1;
	private int m_bonusMoney2;
	private int m_bonusXP;
	private Vector m_bonusItems;
	private static final int HEIGHT_ITEM = 36;
	
	public ListQuestScr() {
		cmdClose = new Command(GameResource.close, new IAction() {
			public void perform() {
				close();
                               // System.gc();
			}
		});		
	}
	
	public void switchToMe(String title, Vector vtQuest, Vector vtBonusItem, int bonusMoney1, int bonusMoney2, int bonusXP, Command leftCmd, Command centerCmd) {
		m_selected=0;
		
		m_cmdLeft = leftCmd;
		m_cmdRight = cmdClose;
		m_cmdCenter = centerCmd;
		
		backScreen = GameCanvas.currentScreen;

		CRes.cleanVector(m_items);
		m_items = vtQuest;
		
		m_bonusMoney1 = bonusMoney1;
		m_bonusMoney2 = bonusMoney2;
		m_bonusXP = bonusXP;
		
		CRes.cleanVector(vtBonusItem);
		m_bonusItems = vtBonusItem;
	
		m_width = GameCanvas.w - 100;
        if(GameCanvas.w <= 240)
        	m_width = 160;
        m_height = GameCanvas.h - 100;
        if(GameCanvas.h <= 200)
        	m_height = 160;
    	m_left = (GameCanvas.w - m_width)/2;
    	m_top = (GameCanvas.h - m_height)/2;
    	
		m_heightListView = vtQuest.size() * HEIGHT_ITEM;
		if(m_heightListView > m_height - 120){
			m_heightListView = m_height - 120;
		}
		
		PaintPopup.gI().setInfo(title, m_width, m_height, 1);
		super.switchToMe();
		init();
	}
	
	public void init(){		
		int tmp = m_items.size() * HEIGHT_ITEM;
		GameCanvas.cameraList.setInfo(20,ITEM_HEIGHT + 100, m_width, HEIGHT_ITEM, m_width, tmp, 
				m_width, m_heightListView,m_items.size());
	}

	public void paint(Graphics g) {		
		g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
		backScreen.paint(g);		
		PaintPopup.paintBoxTab(g, PaintPopup.gI().getName(), m_left-10, m_top-10, m_width+20, m_height+20, 0x9be5ff, 0x3c8dc0);
		paintList(g);				
		super.paint(g);
	}
	
	private void paintList(Graphics g) {		
		int y = 0;
		Position pos = GameCanvas.getMinMaxFor(CameraList.cmy, m_top, m_items.size(),1, m_height);		
		y = HEIGHT_ITEM * pos.x;		
		y = (30 < m_top)?	30 : 0;
		
		g.setClip(m_left,  m_top+ 25, m_left+m_width, m_heightListView);
		g.translate(0, -CameraList.cmy + ITEM_HEIGHT + 25);
		
		int i;
		for (i = pos.x; i < pos.y; i++) {
			if (i == m_selected) {
				PaintPopup.paintSelect(g, m_left, y, m_width , HEIGHT_ITEM);						
			}
			Quest ava = (Quest) m_items.elementAt(i);
			ava.paintInRow(g, m_left, y, m_width, HEIGHT_ITEM, i == m_selected);
			
			y += HEIGHT_ITEM;
		}
		pos = null;
		
		y = m_top + 25 + m_heightListView;
		g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(m_left , y, m_left+m_width, m_height - m_heightListView);
		
		y += 10;
		int x = m_left + 10;
		g.setColor(0x99ffaa);
		g.drawLine(x, y - 5, x + m_width - 20, y - 5);
	
		BitmapFont.drawBoldFont(g, GameResource.phanThuong, x, y, 0xff3e45, Graphics.LEFT);
		x = m_left + 5;
		String str;
		if(m_bonusItems.size() > 0){
			y += 10;
			//x += 10;
            int a = m_bonusItems.size();
			for(i = 0; i < a; i++){
				Item item = (Item)m_bonusItems.elementAt(i);
				item.paintIcon(g, x, y);
				str = item.itemInfo.m_strName+":";
				BitmapFont.drawNormalFont(g, str, x, y+30, 0x3a001e, Graphics.LEFT);
				BitmapFont.drawBoldFont(g, ""+item.m_iNumItem, x+BitmapFont.m_facedeFont.stringWidth(str), y+30, 0x3a001e, Graphics.LEFT);
				str = item.itemInfo.m_strName+":" + item.m_iNumItem;
				x += (BitmapFont.m_facedeFont.stringWidth(str) > 30)?BitmapFont.m_facedeFont.stringWidth(str)+5:35;
			}
		}
		
		x = m_left + 5;
		y += 45;
		str = GameResource.gold +":";
		BitmapFont.drawNormalFont(g, str, x, y, 0x3a001e, Graphics.LEFT);
		BitmapFont.drawBoldFont(g, m_bonusMoney1+"", x+BitmapFont.m_facedeFont.stringWidth(str), y, 0x3a001e, Graphics.LEFT);
		str = GameResource.gold +":" +m_bonusMoney1;
		BitmapFont.drawNormalFont(g, GameResource.space + GameResource.dina +":", x+BitmapFont.m_facedeFont.stringWidth(str), y, 0x3a001e, Graphics.LEFT);
		str = GameResource.gold +":" +m_bonusMoney1+GameResource.space + GameResource.dina +":";
		BitmapFont.drawBoldFont(g, ""+ m_bonusMoney2, x+BitmapFont.m_facedeFont.stringWidth(str), y, 0x3a001e, Graphics.LEFT);
		
		y += 15;
		BitmapFont.drawNormalFont(g, GameResource.exp +":", x, y, 0x3a001e, Graphics.LEFT);
		str = GameResource.exp+":";
		BitmapFont.drawBoldFont(g, m_bonusXP+"", x+BitmapFont.m_facedeFont.stringWidth(str), y, 0x3a001e, Graphics.LEFT);
	}
	
	public void updateKey() {
		super.updateKey();					
	}
	public void addItem(MyObj obj){		
		m_items.addElement(obj);		
		init();		
	}
	public void update() {				
		backScreen.update();
	}
	public MyObj getSelectedItem(){
		if(m_selected >=0 && m_selected < m_items.size())
			return (MyObj)m_items.elementAt(m_selected);
		return null;
	}
	public MyObj getItem(int id){
                int a = m_items.size();
		for(int i=0;i< a;i++){
			MyObj obj=(MyObj)m_items.elementAt(i);
			if(obj.m_iId==id)
				return obj;
		}
		return null;
	}

	public void close() {
		// TODO Auto-generated method stub
		m_cmdCenter = null;
		m_cmdRight = null;
		m_cmdLeft = null;
		
		GameCanvas.cameraList.close();
		
		backScreen.switchToMe();
	}
	
}
